jQuery(document).ready(function() {
    $(document).ready(function() {
   		$("#submit_createGroup").click(function() {
		    alert("shadap");
   		});
 	});
});
 			


/* FINDEXISTINGGROUP(group_name, display_object)
*  this function returns a string containing all groups which matches group_name
*  group_name : name of group we are searching for
*  display_object : name of the label in which the groups are displayed
*/
function findExistingGroup(group_name, display_object)
{

var xmlhttp;
if (group_name.length==0)
  { 
  document.getElementById(display_object).innerHTML="Enter a group's name";
  document.getElementById("btn_createGroup").style.visibility = "hidden";
  document.getElementById("label_passGroup").style.visibility	= "hidden";
  document.getElementById("input_passGroup").style.visibility	= "hidden";	
  document.getElementById("btn_joinGroup").style.visibility	= "hidden";
// document.getElementById("btn_findGroup").disabled = "disabled";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    if (xmlhttp.responseText == -1)
    {
    	document.getElementById(display_object).innerHTML = "No groups found, create group?";
    	document.getElementById("btn_createGroup").style.visibility = "visible";
    	document.getElementById("label_passGroup").style.visibility	= "hidden";
    	document.getElementById("input_passGroup").style.visibility	= "hidden";
    	document.getElementById("btn_joinGroup").style.visibility	= "hidden";
//    	document.getElementById("btn_findGroup").disabled = "disabled";		
    }
    else
    {
    	var s1 = String(xmlhttp.responseText);
    	if (s1.indexOf(',') != -1)
    	{
     		document.getElementById(display_object).innerHTML=xmlhttp.responseText;
    		document.getElementById("btn_createGroup").style.visibility = "hidden";
    		document.getElementById("label_passGroup").style.visibility	= "hidden";
    		document.getElementById("input_passGroup").style.visibility	= "hidden";
    		document.getElementById("btn_joinGroup").style.visibility	= "hidden";	
 //   		document.getElementById("btn_findGroup").disabled = "disabled";
    	}
    	else
    	{		
     		document.getElementById(display_object).innerHTML="<a href=\"javascript:fillWithGroupName()\" id=\"inputGroupJoinName\">" + xmlhttp.responseText + "</a>";
    		document.getElementById("btn_createGroup").style.visibility = "hidden";
 //   		document.getElementById("btn_findGroup").disabled = "false";
    	}
    }
    }
   document.getElementById("GROUPLOADING_OBJECT").innerHTML="";
  }
document.getElementById("GROUPLOADING_OBJECT").innerHTML="loading..";
xmlhttp.open("GET","include/getGroupName.php?q="+group_name,true);
xmlhttp.send();
};

/* SHOWJOINGROUPPASS
 * writes the name of the href to the group
 */
function fillWithGroupName(val)
{	
	    document.getElementById("findgroup").value = document.getElementById("inputGroupJoinName").innerHTML;
}

/* checkValidLogin()
 * this function checks if the login user/pass is valid
 */

function checkValidLogin()
{
	user_name = document.forms[0].user.value;
	user_pass = document.forms[0].pass.value;
	
	/*document.forms[0].pass.value = Aes.Ctr.encrypt (user_pass, '1234' , 128);
	
	dec = Aes.Ctr.decrypt(document.forms[0].pass.value, '1234', 128 );
	
	alert(user_name + " " + user_pass + " " + document.forms[0].pass.value + " " + dec);
	
	return false;*/
}

function submitLoginInfo()
{
/*	if (document.forms[0].pass.value.length > 18) 
		{
			alert("password too long");
			return false;
		}

	setMaxDigits(19);

	key = new RSAKeyPair(
	 "70847948b825b9bdcb674ec2db927fcb",
	 "",
	 "9c234fbd6834976ddb3ba707d1b0ab03"
	);
	
	var cypher = encryptedString(key,document.forms[0].pass.value + "\x01");
	alert(cypher);
	var url = "gofish.php?q=" + cypher;
	alert(url);
	window.location = url;*/
	//alert(document.forms[0].user.value + " " + document.forms[0].pass.value + " good");
	window.location = "gofish.php";
}

function loginGofish()
{
	
}

function submitUserInfo(){	
	try {
		
		var username = document.forms['user-login-form'].elements['edit-user'].value;
		if (username == "")
			throw "Err1";
		var password = document.forms['user-login-form'].elements['edit-pass'].value;
		if (password == "")
			throw "Err1";
		var password2 = document.forms['user-login-form'].elements['edit-pass2'].value;
		if (password == "")
			throw "Err1";
		var nationality = document.forms['user-login-form'].elements['edit-nat'].value;
		if (nationality == "")
			throw "Err1"
		var sex = document.forms['user-login-form'].elements['select-sex'].value;
		if (sex == "")
			throw "Err1";
			
		if (password != password2)
			throw "Err2";
			
		var hPassword = SHA256(password);
		
		/*
		 * TODO: USE A PUBLIC KEY SCHEME TO ENCRYPT hPassword (ex: RSA)
		 */
		
	    var form = document.createElement("form");
	    form.setAttribute("method", "post");
	    form.setAttribute("action", "submitNewUser.php");
	    form.setAttribute("style", "display: none");
        var field = document.createElement("input");
        field.setAttribute("type", "hidden");
        field.setAttribute("name", "username");
        field.setAttribute("value", username);
		form.appendChild(field);
		var field = document.createElement("input");
        field.setAttribute("type", "hidden");
        field.setAttribute("name", "password");
        field.setAttribute("value", hPassword);
		form.appendChild(field);
		var field = document.createElement("input");
        field.setAttribute("type", "hidden");
        field.setAttribute("name", "nationality");
        field.setAttribute("value", nationality);
		form.appendChild(field);
		var field = document.createElement("input");
        field.setAttribute("type", "hidden");
        field.setAttribute("name", "sex");
        field.setAttribute("value", sex);
		form.appendChild(field);
		
		document.body.appendChild(form);
		form.submit();
		document.body.removeChild(form);
	}
  catch(er) {
	  if(er=="Err1")
	    {
	    	document.getElementById('errorMsg').innerHTML = "All fields have to be completed";
	    }
	  if(er=="Err2")
	    {
	    	document.getElementById('errorMsg').innerHTML = "Password do not match";
	    }
	  if(er=="Err3")
	    {
	    alert("Error! The value is not a number");
	    }
	}
}

function SHA256(plaintext)
{
	var digest;
	var hashInputType = "ASCII";
	var hashOutputType = "HEX";
	var hashVariant = "SHA-256";
	var hashObj = new jsSHA( plaintext, hashInputType );
	try {
		digest = hashObj.getHash( hashVariant, hashOutputType);
	} catch(e) {
		return 0;
	}
	return digest;
}
